﻿using System;
using System.Data;
using System.Collections.Generic;
using RenChangYou.Common;
using RenChangYou.Model;

namespace RenChangYou.BLL
{
    /// <summary>
    /// DY_User
    /// </summary>
    public partial class DY_User
    {
        private readonly RenChangYou.DAL.DY_User dal = new RenChangYou.DAL.DY_User();
        public DY_User()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string UserName)
        {
            return dal.Exists(UserName);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(RenChangYou.Model.DY_User model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(RenChangYou.Model.DY_User model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string UserName)
        {

            return dal.Delete(UserName);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string UserNamelist)
        {
            return dal.DeleteList(UserNamelist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public RenChangYou.Model.DY_User GetModel(string UserName)
        {

            return dal.GetModel(UserName);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public RenChangYou.Model.DY_User GetModelByCache(string UserName)
        {

            string CacheKey = "DY_UserModel-" + UserName;
            object objModel = RenChangYou.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(UserName);
                    if (objModel != null)
                    {
                        int ModelCache = RenChangYou.Common.ConfigHelper.GetConfigInt("ModelCache");
                        RenChangYou.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (RenChangYou.Model.DY_User)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<RenChangYou.Model.DY_User> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<RenChangYou.Model.DY_User> DataTableToList(DataTable dt)
        {
            List<RenChangYou.Model.DY_User> modelList = new List<RenChangYou.Model.DY_User>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                RenChangYou.Model.DY_User model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = dal.DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            return dal.GetRecordCount(strWhere);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod


        #region
        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="user"></param>
        /// <returns>1 注册成功 0用户名已存在 2系统异常</returns>
        public int RegisterUser(RenChangYou.Model.DY_User user)
        {
            //return dal.RegisterUser(user);
            if (dal.Exists(user.UserName))
            {
                return 0;
            }
            else
            {
                return dal.Add(user) ? 1 : 2;
            }
        }

        /// <summary>
        /// 检查用户名是否存在
        /// </summary>
        /// <param name="userName"></param>
        /// <returns>1存在，不存在</returns>
        public int CheckTheAccountIsCanUse(string userName)
        {
            //return dal.CheckTheAccountIsCanUse(userName);
            return dal.Exists(userName)==true ? 1 : 0;
        }

        //登陆
        public DataTable UserLogin(string userName, string password)
        {
            return dal.UserLogin(userName, password);
        }


        public int UpdPwd(string account,string oldPassword,string newpassword)
        {
            RenChangYou.Model.DY_User userModel=new Model.DY_User();
            return dal.UpdPwd(account, oldPassword, newpassword);
        }

        
        #endregion

    }
}